package com.sugeun.alarm;

import com.sugeun.stopwatch.R;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;


public class AlarmTimeZoneSettings extends Activity implements OnClickListener{
	private final String TAG = "AlarmTimeZoneSettings"; 
	private ImageButton hour_up_btn;
	private ImageButton hour_down_btn;
	private ImageButton minute_up_btn;
	private ImageButton minute_down_btn;

	
	private Button OkBtn;
	private EditText input_hour;
	private EditText input_minute;

	
	private int hour = 0;
	private int minute = 0;

	
	private String time = "";
	private String mHour = "";
	private String mMinute = "";

	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarm_time_zone);
		time = getIntent().getStringExtra("time");
		mHour = time.substring(0, time.indexOf(":"));
		mMinute = time.substring(time.indexOf(":")+1, time.length());
	//	Log.e(TAG, "mHour : "+mHour+"  // mMinute : "+mMinute);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		
		input_hour = (EditText)findViewById(R.id.input_hour);
		input_hour.setText(mHour);
		input_minute = (EditText)findViewById(R.id.input_minute);
		input_minute.setText(mMinute);
		
		hour_up_btn = (ImageButton)findViewById(R.id.hour_up_btn);   
		hour_up_btn.setOnClickListener(this);
		hour_down_btn = (ImageButton)findViewById(R.id.hour_down_btn); 
		hour_down_btn.setOnClickListener(this);
		minute_up_btn = (ImageButton)findViewById(R.id.minute_up_btn);    
		minute_up_btn.setOnClickListener(this);
		minute_down_btn = (ImageButton)findViewById(R.id.minute_down_btn);  
		minute_down_btn.setOnClickListener(this);

		
		OkBtn = (Button)findViewById(R.id.OkBtn);
		OkBtn.setOnClickListener(this);
		
		hour = Integer.parseInt(input_hour.getText().toString());
		minute = Integer.parseInt(input_minute.getText().toString());

		
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.hour_up_btn :

				 Message hourUp = mHandler.obtainMessage();
				 hourUp.what = 0;
				 mHandler.sendMessage(hourUp);

			break;
		case R.id.hour_down_btn :

				 Message hourDown = mHandler.obtainMessage();
				 hourDown.what = 1;
				 mHandler.sendMessage(hourDown);

			break;
		case R.id.minute_up_btn :

				 Message minuteUp = mHandler.obtainMessage();
				 minuteUp.what = 2;
				 mHandler.sendMessage(minuteUp);

			break;
		case R.id.minute_down_btn :

				 Message minuteDown = mHandler.obtainMessage();
				 minuteDown.what = 3;
				 mHandler.sendMessage(minuteDown);

			break;

		case R.id.OkBtn : 
			
			

			
			Intent intent = getIntent();
			
			intent.putExtra("hour", input_hour.getText().toString());	
			intent.putExtra("minute", input_minute.getText().toString());	

			setResult(RESULT_OK, intent);
			finish();
		
		}
	}
	Handler mHandler = new Handler(){
		 
    	public void handleMessage(Message msg){
   		  	switch (msg.what) {			
				
			case 0:				
				hour++;
				if(hour<10){
					input_hour.setText("0"+hour);
				}else if(hour >= 10 && hour <= 23){
					input_hour.setText(hour+"");
				}else if(hour >= 24){
					hour = 0;
					input_hour.setText("00");
				}
				break;
				
			case 1 : 
				hour--;
				

				if(hour<10 && hour >= 0){
					input_hour.setText("0"+hour);
				}else if(hour >= 10 && hour <= 23){
					input_hour.setText(hour+"");
				}else if(hour <= 0){
					hour = 23;
					input_hour.setText("23");
				}
				break;
				
			case 2 : 
				minute++;

				if(minute<10){
					input_minute.setText("0"+minute);
				}else if(minute >= 10 && minute <= 59){
					input_minute.setText(minute+"");
				}else if(minute >= 60){
					minute = 0;
					input_minute.setText("00");
				}
				break;
				
			case 3 : 
				minute--;

				if(minute<10 && minute >= 0){
					input_minute.setText("0"+minute);
				}else if(minute >= 10 && minute <= 59){
					input_minute.setText(minute+"");
				}else if(minute <= 0){
					minute = 59;
					input_minute.setText("59");
				}
				break;

			
			
			}
 
    	};
 
 };
	
}
